// 动态计算列宽
export function computeColWidth(datalist, col) {
  // console.info('computeWidth', datalist, col);
  let width = 0;
  let single = 11;
  let maxWidth = 380;
  let minWidth = 100;
  let i = 0;
  if (datalist) {
    datalist.forEach((item) => {
      let wh = 0;
      i = i++;
      if (i > 10) return;
      // 通过数据计算该列的长度,默认计算10条数据
      if (item[col] && item[col].toString().length) {
        let chlist = [...item[col].toString()];
        chlist.forEach((ch) => {
          //console.info('computeWidth',ch,isChinese(ch))
          if (isChinese(ch))
            //如果是中文,宽度双倍
            wh += single * 2;
          else wh += single;
        });
      }
      //取每条里面最大的宽度
      width = wh > width ? wh : width;
    });
  }
  if (width < minWidth) width = minWidth;
  else if (width > maxWidth) width = maxWidth;
  // console.info('computeWidth', col,width);
  return width;
}

// 判断是否中文字符
function isChinese(temp) {
  var re = /[^\u4E00-\u9FA5]/;
  if (re.test(temp)) return false;
  return true;
}

// 将毫秒转为天/小时/分钟/秒
export function formatDuring(time) {
  // 避免科学计数法,先转换为Number类型
  let num=new Number(time / (1000 * 60 * 60 * 24));
  let days = parseInt(num.toLocaleString());
  let hours = parseInt((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  let minutes = parseInt((time % (1000 * 60 * 60)) / (1000 * 60));
  let seconds = (time % (1000 * 60)) / 1000;

  return days * 24 + hours + "小时" + minutes + "分" + seconds + "秒";
}

// 将数字格式化,增加逗号
// https://blog.csdn.net/weixin_41697143/article/details/107618088
export function formatNumber(num){
  return Intl.NumberFormat().format(num);
}

// 比较两个对象是否相等,支持null,undefined
export function objectEqual(obj1,obj2){
  return JSON.stringify(obj1) === JSON.stringify(obj2);
}